package com.yjweb.webportal.controller;
import com.yjweb.webportal.entities.Employee;
import com.yjweb.webportal.entities.Pagination;
import com.yjweb.webportal.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by windMan on 2018/5/30
 */
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //查询所有员工返回列表页面
    @GetMapping("/emps")
    public String list(Model model,Integer page) {
        Integer localpage=1;
        if (page!=null){
            localpage=page;
        }
        Pagination<Employee> pagination=employeeService.getAllEmpInfopublic(localpage,10);
        model.addAttribute("indexPage",pagination.getPageInfo().getPageIndex());
        model.addAttribute("totalPage",pagination.getPageInfo().getPageCount());
        //放在请求域中
        model.addAttribute("emps",pagination);
        return "emp/list";
    }


    //来到员工添加页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
        return "emp/add";
    }

    //员工添加
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeService.insertEmp(employee);
        return "redirect:/emps";
    }

    //来到修改页面，查出当前员工，在页面回显
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeService.getEmpById(id);
        model.addAttribute("emp",employee);
        //回到修改页面(保存);
        return "emp/add";
    }

    //员工修改；需要提交员工id；
    @PutMapping("/emp")
    public String updateEmployee(Employee employee){
        employeeService.updateEmp(employee);
        return "redirect:/emps";
    }

    //员工删除
    @DeleteMapping("/emp/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id){
        employeeService.deleteEmp(id);
        return "redirect:/emps";
    }



}
